package com.eqdd.yiqidian.ui.tongxunlu

import android.Manifest
import android.database.Cursor
import android.os.Bundle
import android.provider.ContactsContract
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.bean.ContantsImportBean
import com.eqdd.yiqidian.bean.Person
import com.eqdd.yiqidian.http.DialogCallBack
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.kotlin.KotlinBaseActivity
import com.eqdd.yiqidian.utils.ContactsUtil
import com.eqdd.yiqidian.utils.ImageUtil
import com.eqdd.yiqidian.utils.ToastUtil
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.tbruyelle.rxpermissions.RxPermissions
import kotlinx.android.synthetic.main.activity_contants_import.*
import kotlinx.android.synthetic.main.list_item_143.view.*
import org.jetbrains.anko.toast
import java.util.*

/**
 * Created by jhj on 18-5-21.
 */

class ContantsImportActivity : KotlinBaseActivity() {
    val list = ArrayList<ContantsImportBean>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_contants_import)
        recyclerView.layoutManager = LinearLayoutManager(this)
        initTopBar("一键导入", "确认", View.OnClickListener {
            val s: StringBuilder = StringBuilder()
            if (list.size > 0) {
                list.map { it.userGuid }.forEach {
                    s.append(it).append(",")
                }
            } else {
                return@OnClickListener
            }

            OkGo.post<HttpResult<*>>(HttpConfig.BASE_URL + HttpConfig.ADD_CONTANTS)
                    .params("userGuid", user.guid)
                    .params("friendGuid", s.toString().substring(0, s.length - 1))
                    .params("content", "")
                    .execute(object : DialogCallBack<HttpResult<*>>(this@ContantsImportActivity) {

                        override fun onSuccess(response: Response<HttpResult<*>>) {
                            val httpResult = response.body()
                            toast(httpResult.msg)
                            if (httpResult.status == 200) {
                                finish()
                            }
                        }
                    })
        })
    }


    override fun initData() {
        val builder = StringBuilder()
        val allContacts = ContactsUtil.getMostQuickAllContacts(this)
        allContacts.map {
            val phone = it.phone.replace("-", "").replace(" ", "")
            String.format(Locale.CHINA, "%s-%s", it.phone, phone)
        }.forEach {
            builder.append(it)
                    .append(";")
        }
        OkGo.post<HttpResult<List<ContantsImportBean>>>(HttpConfig.BASE_URL + HttpConfig.IMPORT_CONTANTS)
                .params("userGuid", user.guid)
                .params("para", builder.toString().substring(0, builder.length - 1))
                .execute(object : DialogCallBack<HttpResult<List<ContantsImportBean>>>(this@ContantsImportActivity) {

                    override fun onSuccess(response: Response<HttpResult<List<ContantsImportBean>>>) {
                        val httpResult = response.body()
                        if (httpResult.status == 200) {
                            initUI(httpResult.items)
                        } else {
                            finish()
                        }
                    }
                })
    }

    private fun initUI(items: List<ContantsImportBean>) {

        for (item in items) {
            if (item.isFriend != -2) {
                list.add(item)
            }
        }
        val adapter = ContantsImportAdapter()
        adapter.dataList = list
        recyclerView.adapter = adapter


    }

    override fun setView() {

    }

    class ContantsImportAdapter : RecyclerView.Adapter<ContantsImportAdapter.ItemViewHolder>() {
        var dataList: ArrayList<ContantsImportBean> = arrayListOf();

        override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
            val bean = dataList[position]
            holder?.itemView?.let {
                it.tv_name.text = bean.eqdNickname
                it.tv_number.text = bean.phone
                ImageUtil.setCircleImage(bean.headImage, it.iv_head)
            }
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
            val inflter = LayoutInflater.from(parent?.context)
            return ItemViewHolder(inflter.inflate(R.layout.list_item_143, parent, false))
        }

        override fun getItemCount(): Int {
            return dataList.size
        }

        class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
    }


}
